//
//  ShellNodeHeadView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/24.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellNodeHeadView.h"
#import "ShellNodeViewModel.h"



@interface  ShellNodeHeadView()

@property (nonatomic,strong) ShellNodeViewModel *viewModel;

@end
@implementation ShellNodeHeadView

- (instancetype)initViewModel:(ShellNodeViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellNodeViewModel *)viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    
    ///!!!:头像
    ShellUserDetailModel *userDetail = [LZToolCache getUserInfo];
    UIImageView *headImgView = [[UIImageView alloc] init];
    [self addSubview:headImgView];
    [headImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(27);
        make.width.height.mas_equalTo(70);
        make.left.mas_equalTo(28);
    }];
    [headImgView sd_setImageWithURL:[NSURL URLWithString:userDetail.portrait] placeholderImage:k_imageName(@"default_headportrait")];
    headImgView.cornerRadius = 35;
       
    ///!!!:名字
    UILabel *nameLab = [UILabel lz_labelWithText:userDetail.name fontSize:k_textBoldFont(18) color:UIColorHex(0xFFFFFF)];
    [self addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(33);
        make.width.mas_equalTo(170);
        make.height.mas_equalTo(25);
        make.left.mas_equalTo(headImgView.mas_right).mas_offset(15);
    }];
       
       
    ///!!!:等级
    UIButton *levelBtn = [UIButton lz_buttonImageName:@"" backgroundImageName:@""];
    [levelBtn setGradientBackgroundWithColors:@[UIColorHex(0xFFE482),UIColorHex(0xFF9559)] locations:@[@(0),@(1.0)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [self addSubview:levelBtn];
    [levelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.mas_equalTo(nameLab.mas_bottom).mas_offset(7);
       make.width.mas_equalTo(70);
       make.height.mas_equalTo(20);
       make.left.mas_equalTo(headImgView.mas_right).mas_offset(15);
    }];
    levelBtn.cornerRadius = 10;

       
    UIImageView *levelImgView = [[UIImageView alloc] initWithImage:k_imageName(@"icon_zuanshi")];
    [levelBtn addSubview:levelImgView];
    [levelImgView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerY.mas_equalTo(levelBtn.mas_centerY);
       make.width.mas_equalTo(20);
       make.height.mas_equalTo(15);
       make.left.mas_equalTo(6);
    }];

    UILabel *levelLab = [UILabel lz_labelWithText:self.viewModel.levelDictionary[@"levelString"] fontSize:k_textFont(12) color:UIColorHex(0xFFFFFF)];
    [levelBtn addSubview:levelLab];
    [levelLab mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerY.mas_equalTo(levelBtn.mas_centerY);
       make.width.mas_equalTo(45);
       make.height.mas_equalTo(17);
       make.left.mas_equalTo(levelImgView.mas_right).mas_offset(2);
    }];
       
    
    //创建
    UIProgressView *progressView = [[UIProgressView alloc] init];
    //背景颜色
    progressView.backgroundColor = UIColorHex(0x0B2846);
    //样式
    progressView.progressViewStyle = UIProgressViewStyleDefault;
    //已完成进度颜色
    progressView.progressTintColor = UIColorHex(0x4AB2E7);

    [self addSubview:progressView];
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(levelBtn.mas_centerY);
        make.left.mas_equalTo(levelBtn.mas_right).mas_offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(6);
    }];
    progressView.cornerRadius = 3;
        
    NSString *brokerComplete = [NSString stringWithFormat:@"%@",self.viewModel.levelDictionary[@"brokerComplete"]];
    NSString *brokerNeed = [NSString stringWithFormat:@"%@",self.viewModel.levelDictionary[@"brokerNeed"]];
    //现在的进度
    progressView.progress = [brokerComplete floatValue]/[brokerNeed floatValue];
    

    UILabel *progressLabel = [UILabel lz_labelWithText:[NSString stringWithFormat:@"%@ / %@",brokerComplete,brokerNeed] fontSize:k_textFont(10) color:UIColorHex(0x8D99B2)];
    [self addSubview:progressLabel];
     [progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerY.mas_equalTo(levelBtn.mas_centerY);
         make.left.mas_equalTo(progressView.mas_right).mas_offset(10);
         make.width.mas_equalTo(50);
         make.height.mas_equalTo(15);
     }];
    
    
    ///算力
    NSString *hashRate = [NSString stringWithFormat:@"%@",self.viewModel.levelDictionary[@"hashRate"]];
    NSString *hashRateString = [NSString stringWithFormat:@"%@: %@",LZLocalizedString(@"算力"),hashRate];
    NSMutableAttributedString *hashRateAttributedString = [NSAttributedString attributedString:hashRateString rangeTitle:hashRate leftFont:k_textFont(12) rightFont:k_textFont(12) leftColor:UIColorHex(0x8D99B2) rightColor:UIColorHex(0xFFFFFF)];
    UILabel *hashRateLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    [self addSubview:hashRateLab];
    [hashRateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headImgView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(28);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(17);
    }];
    hashRateLab.attributedText = hashRateAttributedString;
    
    
    ///节点算力
    NSString *allHashRate = [NSString stringWithFormat:@"%@",self.viewModel.levelDictionary[@"allHashRate"]];
    NSString *allHashRateString = [NSString stringWithFormat:@"%@: %@",LZLocalizedString(@"节点算力"),allHashRate];
    NSMutableAttributedString *allHashRateAttributedString = [NSAttributedString attributedString:allHashRateString rangeTitle:allHashRate leftFont:k_textFont(12) rightFont:k_textFont(12) leftColor:UIColorHex(0x8D99B2) rightColor:UIColorHex(0xFFFFFF)];
    UILabel *allHashRateLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    [self addSubview:allHashRateLab];
    [allHashRateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headImgView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(180);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(17);
    }];
    
    allHashRateLab.attributedText = allHashRateAttributedString;

    
    
    
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = UIColorHex(0x8D99B2);
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headImgView.mas_bottom).mas_offset(60);
        make.left.mas_equalTo(25);
        make.width.mas_equalTo(K_SCREENWIDTH - 50);
        make.height.mas_equalTo(1);
    }];
    
    ///!!!:等级说明
    UILabel *levelTipLab = [UILabel lz_labelWithText:LZLocalizedString(@"等级说明") fontSize:k_textBoldFont(16) color:UIColorHex(0xFFFFFF)];
    [self addSubview:levelTipLab];
    [levelTipLab mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.mas_equalTo(lineView.mas_bottom).mas_offset(25);
       make.left.mas_equalTo(25);
       make.width.mas_equalTo(200);
       make.height.mas_equalTo(22);
    }];

    NSString *levelTextDesc = [NSString stringWithFormat:@"%@",self.viewModel.levelDictionary[@"levelTextDesc"]];
    UILabel *tipLabel = [UILabel lz_labelWithText:levelTextDesc fontSize:k_textFont(13) color:UIColorHex(0x8D99B2)];
    [self addSubview:tipLabel];
    CGFloat tipHeight = [levelTextDesc lz_textHeightWithFontSize:k_textFont(13) withMaxWidth:K_SCREENWIDTH - 50];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.mas_equalTo(levelTipLab.mas_bottom).mas_offset(8);
       make.left.mas_equalTo(25);
       make.width.mas_equalTo(K_SCREENWIDTH - 50);
       make.height.mas_equalTo(tipHeight);
    }];
}



@end
